import api from "../../common/api.js";
import util from "../../utils/util.js";
import {
  getDate,
  getzf
} from "../../utils/utilsNew.js";
import p from "../../utils/platform.js";
import * as modules from "../../models/modules";
var app = getApp();
Component({
  /**
   * 页面的初始数据
   */
  data: {
    isShowMemberCenter: true,
    imgArray: '',
    heightVi: app.globalData.windowHeight,
    widthVi: app.globalData.windowWidth,
    name: '',
    tel: '',
    address: '',
    floor: '',
    wechatInfoState: false, //个人名片是否填写
    sessionInfo: '',
    setState: false,
    storeName: "",
    wechatQrCode: '', //微信二维码
    white: false, //是否为白名单
    cash: '', //账户余额
    isShowOfficial: true,
    changHeadImg: false, //是否显示修改头像
    changName: true, //是否显示修改姓名
    company: false, // 门店品牌和地址是否可更改
    applyStatus: "0", // 申请加入企业状态
    contactStyle: false, //联系客服的方式
    height: '', //屏幕刘海高度
    kefu: {},
    statue: null,
  },

  methods: {
    hanldeOfficialLoad(e) {
      console.log("关注公众号组件load====", e);
    },

    hanldeOfficialError(e) {
      console.log("关注公众号组件error====", e);
      this.setData({
        isShowOfficial: false
      })
    },
    // 去我的企业
    goCompany() {
      if (this.data.applyStatus == "0") {
        wx.navigateTo({
          url: '/pages/mine/myCompany/myCompany'
        })
      } else if (this.data.applyStatus == "1") {
        wx.navigateTo({
          url: '/pages/mine/myCompany/myCompany?status=1'
        })
      } else if (this.data.applyStatus == "2") {
        wx.navigateTo({
          url: '/pages/mine/myFirm/myFirm'
        })
      }
    },
    goCoupons() {
      wx.navigateTo({
        url: '/pages/mine/myCoupons/myCoupons',
      })
    },
    // 查询申请企业状态
    goMycompany() {
      api.getBindShop({}, (res) => {
        if (res.data.isSuccess) {
          wx.setStorageSync('company', {
            shopAllName: res.data.content.shopAllName,
            id: res.data.content.shopId,
            storeAddress: res.data.content.storeAddress,
            storeAllName: res.data.content.storeAllName,
            shopShortName: res.data.content.shopShortName,
          })
          this.setData({
            applyStatus: res.data.content.bindStatus
          })
        } else {
          wx.showToast({
            title: '查询企业结果失败',
            icon: "none"
          })
        }
      })
    },
    handleClose() {
      this.setData({
        isShowOfficial: false
      })
    },
    hanldeKefu() {
      p.makePhoneCall({
        phoneNumber: this.data.kefu.serviceMobile
      })

      api.insertMinePageOperationLog({
        logType: "0"
      })
    },
    goQiwei() {
      wx.navigateTo({
        url: `/pages/webView/webView?webViewUrl=${this.data.kefu.qwLinkUrl}`
      })
    },
    goMyKefu() {
      if (this.data.kefu.privateStatue) {
        let kefu = JSON.stringify(this.data.kefu)
        wx.navigateTo({
          url: `/pages/newUser/10086/10086?kefu=${kefu}`
        })
      } else if (!this.data.kefu.privateStatue && this.data.kefu.qwLinkUrl) {
        this.goQiwei();
      } else {
        return
      }
    },
    //企业认证
    goAuthentication() {
      wx.navigateTo({
        url: '/pages/mine/authentication/index'
      })
    },
    //我的账户
    goMyaccount() {
      wx.navigateTo({
        url: '/pages/mine/myAccount/myAccount'
      })

    },
    //选择图片
    chooseimage() {
      var that = this;
      wx.navigateTo({
        url: '/pages/mine/personData/personData',
      })
      if (wx.getStorageSync('myInformation')) {
        wx.removeStorageSync('myInformation')
      }

    },
    //跳转设置详情
    goSetDetail(e) {
      var title = e.currentTarget.dataset.title;

      if (title == "主营楼盘") {
        wx.navigateTo({
          url: '/pages/addHouse/searchHouse/searchHouse?mainBuilding=1',
        })
      } else {
        wx.navigateTo({
          url: '/pages/mine/setDetail/setDetail?title=' + title,
        })
      }
    },
    // 判断是否为白名单用户
    isWihteList() {
      let that = this;
      api.isWhiteList({
        "activityCode": "InviteFriends"
      }, res => {
        that.setData({
          // white: res.data.isSuccess==false?false:true
          white: res.data.isSuccess
        })
      })
    },
    // 用户余额
    myBalance() {
      let that = this;
      api.myBalance({}, res => {
        that.setData({
          cash: res.data.content.availableAmount
        })
      })
    },
    //跳转个人名片
    goSetWechatDetail() {
      wx.navigateTo({
        url: '/pages/mine/setWechatDetail/setWechatDetail?wechatQrCode=' + this.data.wechatQrCode,
      })
    },
    //跳转个人网络店铺
    goOnlineShop() {
      let that = this
      if (that.data.storeName == null || that.data.address == null || that.data.storeName == '' || that.data.address == '') {
        wx.navigateTo({
          url: '/pages/mine/myInformation/myInformation'
        })
      } else {
        wx.navigateTo({
          url: '/pages/mine/onlineShop/onlineShop'
        })
      }
    },
    // 获取会员到期时间 
    getMemberList() {
      let memberAgentId = getApp().globalData.loginUserInfo.data.content.sessionUser.id;
      api.getMemberList({
        "currentPage": 1,
        "pageSize": 1,
        "t": {
          "memberAgentId": memberAgentId
        }
      }, (res) => {
        let time = getDate('yyyy/mm/dd', res.data.content.endDate);
        let startDate = getDate('yyyy-mm-dd', res.data.content.rechargeDate);
        this.setData({
          endDate: time,
          startDate: startDate,
          statue: res.data.content.statue
        })
      })
    },
    //获取设置页面数据
    getSessionInfo() {
      return new Promise((resolve, reject) => {
        this.goMycompany();
        this.getMemberList();
        this.getPrivateSale();
        // this.renderShowMemberCenter();
        modules.updateMemberCenter()
        .then((res) => {
          this.setData({
            isShowMemberCenter: res === '1' ? true : false,
          })
        })
        var that = this;
        api.getSessionInfo(null, function (res) {
          var data = res.data.content,
            floor = data.extProps.resblock,
            newFloor = [];
          floor.forEach((item, index) => {
            if (item.hdResblockName && item.hdResblockName != '') {
              newFloor.push(item.hdResblockName)
            } else {
              newFloor.push(item.resblockName);
            }
          })
          if (that.data.applyStatus == '2') {
            let company = wx.getStorageSync('company')
            that.setData({
              company: true
            })
            var storeName = company.shopShortName;
            var address = company.storeAddress;
          } else {
            var storeName = data.extProps.storeName != 'null' ? data.extProps.storeName : '';
            var address = data.extProps.storeAdress != 'null' ? data.extProps.storeAdress : '';
          }
          that.setData({
            imgArray: data.avatar,
            name: data.realName == 'null' ? data.username : data.realName,
            tel: data.mobile != 'null' ? data.mobile : '',
            address,
            floor: newFloor.join(" | "),
            wechatInfoState: data.extProps.wechatQrCode == null ? false : true, //微信二维码是否填写
            wechatQrCode: data.extProps.wechatQrCode, //微信二维码
            sessionInfo: res.data.content,
            storeName,
          })
          var settingInfo = {
            imgArray: data.avatar,
            name: data.realName == null ? data.username : data.realName,
            tel: data.mobile,
            address,
            floor: data.extProps.resblock,
            storeName,
          };

          wx.setStorageSync("settingInfo", settingInfo)
          resolve();
          app.updateSessionUser(res.data.content)
        })
        that.isWihteList()
        that.myBalance()
      })
    },

    shezhi: function () {
      wx.setStorageSync("resblockName", '')
      wx.navigateTo({
        url: '/pages/mine/setting/setting',
      })
    },

    slPublicnumber: function () {
      // debugger
      // wx.navigateTo({
      //   url: '/pages/visitors/visitors',
      // })
    },
    // 去充值页面
    goToMemberCentre() {
      wx.navigateTo({
        url: '/pages/mine/memberCentre/memberCentre?statue=' + this.data.statue + '&endDate=' + this.data.endDate + '&startDate=' + this.data.startDate + '&img=' + this.data.imgArray + '&name=' + this.data.name + '&orderEntr=payEntr',
      })
    },


    queryNumber() {
      var that = this;
      that.getSessionInfo();
    },
    openContact() {
      this.setData({
        contactStyle: true
      })
    },
    closeContact() {
      this.setData({
        contactStyle: false
      })
    },
    getPrivateSale() {
      api.getPrivateSale({}, res => {
        this.setData({
          kefu: res.data.content
        })
      })
    },
    //推荐有礼
    goFission() {
      wx.navigateTo({
        url:'/pages/mine/newInvite/newFissionPage/newFissionPage?actEntre=inviteEntr'
      })
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  attached: function () {
    wx._setting = this

    this.setData({
      height: wx.getStorageSync('systemInfo').height
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // debugger
    wx.setStorageSync("isUpdateMine", true);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {}
})